package com.ftg.learn.chapter07;

import java.util.Arrays;
import java.util.Scanner;

/**
 * 习题
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/7/19 下午3:08
 */
public class HomeWork {

    //    定义一个数组内容为{1,2,3},要求输出结果为：[word1#word2#word3]。
    //数组转字符串
    public static String q1(int[] arr) {
        StringBuilder stringBuilder = new StringBuilder("[");
        for (int i = 0; i < arr.length; i++) {
            stringBuilder.append("word").append(arr[i]).append("#");
        }
        return stringBuilder.append("]").toString();
    }

    //、键盘输入一个字符串，并且统计其中各种字符出现的次数。种类有：大写字母、小
    //写字母、数字、其他。
    public static void q2() {
        Scanner scanner = new Scanner(System.in);
        String word = scanner.next();

        StringBuilder stringBuilder = new StringBuilder(word);
        while (stringBuilder.length() > 0) {
            String firstLetter = stringBuilder.substring(0, 1);
            int sum = 0;
            for (int i = stringBuilder.indexOf(firstLetter); i != -1; i = stringBuilder.indexOf(firstLetter, i + 1)) {
                sum++;
            }
            System.out.printf("字母%s出现的次数为:%d\n", firstLetter, sum);
            stringBuilder = new StringBuilder(stringBuilder.toString().replace(firstLetter, ""));
        }
    }

    public static void q2_1() {
        Scanner scanner = new Scanner(System.in);
        String word = scanner.next();

        StringBuilder stringBuilder = new StringBuilder(word);
        while (stringBuilder.length() > 0) {
            int len = stringBuilder.length();
            String firstLetter = stringBuilder.substring(0, 1);
            stringBuilder = new StringBuilder(stringBuilder.toString().replace(firstLetter, ""));
            int sum = len - stringBuilder.length();
            System.out.printf("字母%s出现的次数为:%d\n", firstLetter, sum);

        }
    }

    public static void q2_2() {
        Scanner scanner = new Scanner(System.in);
        String word = scanner.nextLine();
        int f = 0;
        int s = 0;
        int t = 0;
        int four = 0;
        for (int i = 0; i < word.length(); i++) {
            if (word.charAt(i) >= '0' && word.charAt(i) <= '9') {
                f++;
            } else if (word.charAt(i) >= 'a' && word.charAt(i) <= 'z') {
                s++;
            } else if (word.charAt(i) >= 'A' && word.charAt(i) <= 'Z') {
                t++;
            } else {
                four++;
            }
        }
        System.out.println("数字有" + f + "个");
        System.out.println("小写字母有" + s + "个");
        System.out.println("大写字母有" + t + "个");
        System.out.println("其它字符" + four + "个");
    }

    //    、分析以下需求,并用代码实现
//    键盘录入一个字符串，要求删除该字符串中的所有java字符串（最终的字符串中
//    不能包含java），要求打印删除后的结果以及删除了几个java字符串比如键盘录入:
//    javawoaijava,ilikejajavavaienjoyjava程序输出结果:原字符串woai,i
//    likeienjoy中总共包含:5个java删除java后的字符串为:woai,ilikei
//            enjoy
    public static void q3(String java) {
        Scanner scanner = new Scanner(System.in);
        String word = scanner.nextLine();
        int len = word.length();

        while (word.contains(java)) {
            word = word.replace(java, "");
        }

        System.out.println(word);
        System.out.println((len - word.length()) / 4);
    }

    //    按需求完成指定功能，题目如下：
//            (1)打印由7，8，9三个数组组成的三位数，要求该三位数中任意两位数字不能相
//    同
//            (2)打印格式最后的三位数字以空格分隔，如789798879897978987
    public static void q5() {
        int[] ar = {7, 8, 9};
        for (int i = 0; i < ar.length; i++) {
            int first = 0;
            int second = 0;
            switch (i) {
                case 0:
                    first = i + 1;
                    second = i + 2;
                    break;
                case 2:
                    first = i - 1;
                    second = i - 2;
                    break;
                default:
                    first = i - 1;
                    second = i + 1;
            }
            System.out.printf("%d %d %d\t", ar[i], Math.max(ar[first], ar[second]), Math.min(ar[first], ar[second]));
            System.out.printf("%d %d %d\t", ar[i], Math.min(ar[first], ar[second]), Math.max(ar[first], ar[second]));
        }
    }

    public static void q6(){
        String[] strs = {"010","12321"};
        for (int i = 0; i < strs.length; i++) {
        }
                
    }

    static int[] as = null;
    public static void main(String[] args) {
        q5();
        //运行异常
        double[] d[]=new double[4][];
        System.out.println(as[6]);
    }

    // 10 6 7 8 9（通用性的问题）
    // {1,2,3,4,5} 15
    // 中间项 中间下标   {3,1,2,6,5,1,6}  {1} {3,0} -10 10 {3,-1,4,1,1,3,4,5,}


//    某个公司采用公用电话传递数据信息，数据是小于8位的整数，为了确保安全，
//    在传递过程中需要加密，加密规则如
//    intnumber=123456
//    B:加密规则
//4.Integer和int的区别a:首先将数据倒序
//            结果654321
//    b:然后将每位数字都加上5，再用和除以10的余数代替该数字
//            结果109876
//c:最后将第一位和最后一位数字交换
//            结果609871
//    C:把加密后的结果输出在控制台
    public static void q7(int x){
        if(x < 9 || x > Math.pow(10,7)){
            throw new RuntimeException("对不起不是8位");
        }

        String xStr = String.valueOf(x);
        StringBuilder stringBuilder = new StringBuilder(xStr);
        String strNew = stringBuilder.reverse().toString();

        StringBuilder reStr = new StringBuilder();
        for (int i = 0; i < strNew.length(); i++) {
            int temp = Integer.valueOf(strNew.substring(i,i+1));
            reStr.append((temp + 5) % 10);
        }

        //
        reStr.append(reStr.charAt(0));
        reStr.delete(0,1);
        reStr.insert(0,reStr.charAt(reStr.length()-2));
        reStr.delete(reStr.length()-2,reStr.length()-1);
        System.out.println(reStr);
    }


}
